package ac.manchester.cs.afzal.term_classifier.miscellaneous;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Vector;

/**
 * Occurrences_Generator_Impl class extracts the occurrences from "clist" files.
 * Two parameters, CLISTS_DIR and LSTS_DIR can be set in properties file.
 *
 * <p>Company: The University of Manchester</p>
 *
 * @author Hammad Afzal
 * @version 1.0
 * 
 */
public class Occurrences_Generator_Impl
{

    private Properties props;
    private String CLISTS_DIR = "CLISTS_DIR";
    private String LSTS_DIR = "LSTS_DIR";
    private File_Manager_Impl fileManager = new File_Manager_Impl();
    private Vector_Builder_Impl vectorBuilder = new Vector_Builder_Impl();

    /* CLIST files are generated from the Termine, a software
    for automatic term recognition. It contains the terms and their occurrences
     */
    public Occurrences_Generator_Impl()
    {
        try
        {
            props = new Properties();
            props.load(new FileInputStream("Term_Classification.properties"));
            CLISTS_DIR = props.getProperty(CLISTS_DIR);
            LSTS_DIR = props.getProperty(LSTS_DIR);
        }
        catch (IOException ex)
        {
            Logger.getLogger(Occurrences_Generator_Impl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void lst_files_generator()
    {
        File clists_dir = new File(CLISTS_DIR);
        String[] clists_dir_array = clists_dir.list();

        for (String clists_file : clists_dir_array)
        {
            System.out.println("File: " + clists_file + "being processed");
            if (!(clists_file.startsWith("."))) // Check for hidden folder, specially .svn
            {
                try
                {
                    BufferedReader in = new BufferedReader(new FileReader(CLISTS_DIR + clists_file));
                    Vector<String> occurrences_vector = new Vector();
                    String str = "";
                    while ((str = in.readLine()) != null)
                    {
                        int occurrences_index_start = str.indexOf("\"");
                        int occurrences_index_end = str.indexOf("\"", occurrences_index_start + 1);
                        String occurrences = str.substring(occurrences_index_start + 1, occurrences_index_end - 1);
                        Vector<String> occurrences_vector_single = vectorBuilder.string_to_vector(occurrences, "|");

                        for (String vector_item : occurrences_vector_single)
                        {
                            occurrences_vector.add(vector_item);
                        }
                    }

                    String output_contents = vectorBuilder.vector_to_string_with_items_list(occurrences_vector);

                    String input_file_without_extension = fileManager.strip_file_name_from_file_name_plus_extension(clists_file, "clist");
                    String output_file = input_file_without_extension + ".lst";
                    BufferedWriter out = new BufferedWriter(new FileWriter(LSTS_DIR + output_file));
                    out.write(output_contents);
                    out.close();

                }

                catch (FileNotFoundException ex)
                {
                    Logger.getLogger(Occurrences_Generator_Impl.class.getName()).log(Level.SEVERE, null, ex);
                }                catch (IOException ex)
                {
                    Logger.getLogger(Occurrences_Generator_Impl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            System.out.println("File: " + clists_file + " Processed");
        }
    }
}
